import re

if __name__ == '__main__':
    text = 'This is some text -- with punctuation.'

    print(text)
    print()

    patterns = [
        # (r"^\w+", "匹配字符串开始的词"),
        (r"^(\w+)", "匹配字符串开始的词"),
        # (r"^(\w)+", "匹配字符 s"),
        # (r"^((\w)+)", "匹配字符 s"),
        (r"\b(\w+)\S*$","匹配字符串结束的词"),
        (r"\b(t\w+)\W+(\w+)","匹配 text -- with "),
        (r"(\w+t\b)",""),

    ]

    for pattern, desc in patterns:
        regex = re.compile(pattern)
        match = regex.search(text)
        s = match.start()
        e = match.end()
        substr = text[s:e]
        n_backslashes = text[:s].count('\\')
        prefix = '.' * (s + n_backslashes)
        print("'{}' ({})".format(pattern, desc))
        print("  {}'{}'".format(prefix, substr))
        print('  ', match.groups())
        print()
